<template>
  <vxe-modal v-model="dialogVisible" append-to-body title="收样单新增" width="60%" height="600" :before-hide-method="clear"
    :destroy-on-close="true">
    <p class="common-title"><span class="titleName">合同信息</span></p>
    <Form ref="form" v-model="form" :formRules="contractFormRules" :formOptions="contractFormOptions"
      :collapse-status="form.entrustType !== 'JSW' && !form.isSupplement" />

    <p class="common-title"><span class="titleName">检测信息</span></p>
    <div class="anthorContent-item">
      <Form ref="detectionForm" v-model="detectionForm" :isContractPrice="form.pricingType === 'CONTRACT_PRICE'"
        :contractId="form.contractId" :infoId="form.infoId" :formRules="detectionFormRules"
        :formOptions="detectionFormOptions" @getProject="getProject" />
      <div style="height: 20px" />
      <Table ref="settleRecord" :columns="tableColumn" :tableData="detectionForm.tableData" />
    </div>

    <div slots="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </vxe-modal>
</template>
<script>
import VXETable from 'vxe-table';
import Form from '/@/components/PartForDialog/form.vue';
import Table from '/@/components/PartForDialog/table.vue';
export default {
  components: { Table, Form },
  data() {
    return {
      dialogVisible: false,
      form: {
        entrustType: 'JTW',
        isExternal: false,
        isSupplement: false,
        contractName: '',
        contractId: null,
        infoId: null,
        pricingType: '',
        entrustDate: new Date(),
        companyName: '',
        projectName: '',
        reportNumber: null,
        entrustNumber: null,
      },
      contractFormOptions: {
        column: 3,
        items: [
          {
            title: '委托类型',
            field: 'entrustType',
            type: 'select',
            options: [
              { value: 'JTW', label: '交委' },
              { value: 'JSW', label: '建委' },
              { value: 'CMA', label: 'CMA' },
            ],
          },
          {
            title: '收样单类型',
            field: 'isExternal',
            type: 'radio',
            options: [
              { label: '收样单', value: false },
              { label: '外检单', value: true },
            ],
          },
          {
            title: '是否补录',
            field: 'isSupplement',
            type: 'select',
            options: [
              { label: '是', value: true },
              { label: '否', value: false },
            ],
          },
          {
            title: '合同名称',
            field: 'contractName',
            type: 'selectTable',
            prop: 'contract',
          },
          {
            title: '工程名称',
            field: 'projectName',
            type: 'input',
            disabled: true,
          },
          {
            title: '委托日期',
            field: 'entrustDate',
            type: 'date',
          },
          {
            title: '报告编号',
            field: 'reportNumber',
            type: 'input',
            folding: true,
          },
          {
            title: '委托编号',
            field: 'entrustNumber',
            type: 'input',
            folding: true,
          },
          {
            content: '查重',
            type: 'button',
            folding: true,
            size: 'small',
            status: 'primary',
            event: this.checkDuplicate,
          },
        ],
      },
      contractFormRules: {
        contractName: [{ required: true, message: '合同必选' }],
        reportNumber: [{ required: true, message: '报告编号必填' }],
        entrustNumber: [{ required: true, message: '委托编号必填' }],
      },
      detectionForm: {
        infoId: null,
        chargeStdInfoId: null,
        chargeStdItemId: null,
        chargeStdItemName: null,
        specModel: null,
        manufacturer: '',
        engineeringPart: '',
        batchNumber: '',
        entrustName: '',
        checkParams: [],
        tableData: [],
      },
      detectionFormOptions: {
        column: 3,
        items: [
          {
            title: '检测项目',
            field: 'chargeStdItemName',
            type: 'selectTable',
            prop: 'project',
          },
          {
            title: '规格型号',
            field: 'specModel',
            type: 'input',
          },
          {
            title: '生产厂家',
            field: 'manufacturer',
            type: 'input',
          },
          {
            title: '工程部位',
            field: 'engineeringPart',
            type: 'input',
          },
          {
            title: '批号',
            field: 'batchNumber',
            type: 'input',
          },
          {
            title: '委托人',
            field: 'entrustName',
            type: 'input',
            disabled: true,
          },
        ],
      },
      detectionFormRules: {
        chargeStdItemName: [{ required: true, message: '检测项目必选' }],
      },
      tableColumn: [
        {
          type: 'seq',
          width: 60,
          title: '序号',
        },
        {
          field: 'paramName',
          title: '检测参数',
          editRender: {},
        },
        {
          field: 'salesUnit',
          title: '计价单位',
          editRender: {},
        },
        {
          field: '',
          title: '数量',
          editRender: {},
          slots: {
            default({ row }, h) {
              return h('span', [1]);
            },
          },
        },
        {
          field: 'workingHours',
          title: '单项检测工时',
          editRender: {},
        },
        {
          field: 'detectionStdList',
          title: '检测标准',
          slots: {
            default({ row }, h) {
              function getDetectionStdList(list) {
                return list?.map((item) => item.dictValue)?.join('、');
              }
              return h('span', [getDetectionStdList(row.detectionStdList)]);
            },
          },
        },
        {
          field: 'remarks',
          title: '备注',
          editRender: {},
        },
      ],
    };
  },
  computed: {
    selRowids() {
      return this.value.map((item) => item.id?.toString());
    },
  },
  watch: {
    'form.contractId'(val) {
      if (val) {
        this.detectionForm.infoId = null;
        this.detectionForm.chargeStdInfoId = null;
        this.detectionForm.chargeStdItemId = null;
        this.detectionForm.chargeStdItemName = null;
        this.detectionForm.checkParams = [];
        this.detectionForm.tableData = [];
        this.detectionForm.entrustName = this.form.entrustName;
      }
    },
  },
  mounted() { },
  methods: {

    back() {
      this.$router.go(-1);
    },
    search() {
      this.form.page = 1;
      this.$emit('search', this.form);
    },
    reset() {
      this.resetPage();
      this.$emit('search', this.form);
    },
    clear() {
      this.form = {
        entrustType: 'JTW',
        isExternal: false,
        isSupplement: false,
        contractName: '',
        contractId: null,
        infoId: null,
        pricingType: '',
        entrustDate: null,
        projectName: '',
        reportNumber: null,
        entrustNumber: null,
      };
      this.detectionForm = {
        infoId: null,
        chargeStdInfoId: null,
        chargeStdItemId: null,
        chargeStdItemName: null,
        specModel: null,
        manufacturer: '',
        engineeringPart: '',
        batchNumber: '',
        entrustName: '',
        checkParams: [],
        tableData: [],
      };
      this.dialogVisible = false;
    },
    getProject(tree) {
      const item = tree[0];
      this.detectionForm.chargeStdInfoId = item.infoId;
      this.detectionForm.chargeStdItemId = item.itemId;
      this.detectionForm.chargeStdItemName = item.itemName;
      this.detectionForm.sampleOrderDetailCmdList = item.chargeParamList.map((item) => {
        return {
          detectionStdId: item.detectionStdIds[0],
          paramId: item.paramId,
        };
      });
      this.detectionForm.checkParams = tree;
      this.detectionForm.tableData = item.chargeParamList;
    },
    async save() {
      const error = await this.$refs.form.$refs.form.validate().catch((errMap) => errMap);
      const error1 = await this.$refs.detectionForm.$refs.form.validate().catch((errMap) => errMap);
      if (error || error1) {
        return;
      }
      const { isSupplement, entrustType, reportNumber, entrustNumber, ...rest } = this.form;
      const formData = entrustType === 'JSW' || isSupplement ? this.form : { isSupplement, entrustType, ...rest };
      this.$api.addSampleOrder({ ...formData, ...this.detectionForm }).then((res) => {
        VXETable.modal.message({
          content: '新增成功',
          status: 'success',
        });
        this.$emit('reload');
        this.dialogVisible = false;
      });
    },
    change({ type, currentPage, pageSize }) {
      this.form.page = currentPage;
      this.form.size = pageSize;
      this.$emit('pager', this.form);
    },
    // 重置参数
    resetPage() {
      this.form = {
        employeeName: '',
        idCard: '',
        page: 1,
        size: 10,
      };
    },
    async checkDuplicate() {
      const reportNumber = this.form.reportNumber;
      const entrustNumber = this.form.entrustNumber;

      if (!reportNumber && !entrustNumber) {
        VXETable.modal.message({
          content: '请先填写数据',
          status: 'error'
        });
        return;
      }

      if (reportNumber) {
        const a = await this.$api.isReportNumberDuplicate({ reportNumber }).then((res) => res.data);
        if (a && a !== null) {
          VXETable.modal.message({
            content: '报告编号重复',
            status: 'error'
          });
        } else {
          VXETable.modal.message({
            content: '报告编号未重复',
            status: 'error'
          });
        }
      }

      if (entrustNumber) {
        const b = await this.$api.isEntrustNumberDuplicate({ entrustNumber }).then((res) => res.data);
        if (b && b !== null) {
          VXETable.modal.message({
            content: '委托编号重复',
            status: 'error'
          });
        } else {
          VXETable.modal.message({
            content: '委托编号未重复',
            status: 'error'
          });
        }
      }
    }
  }
};
</script>



<style lang="scss" scoped>
::v-deep {
  .table-normal .vxe-body--row .vxe-body--column {
    height: 40px !important;
  }

  .vxe-modal--box {
    position: absolute;
    left: 50% !important;
    top: 50% !important;
    transform: translate(-50%, -50%) !important;
  }
}
</style>
